package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.courseware.exercise.PlannedSchedule;
import co.fitstart.entity.courseware.fitnessgroup.Courseware;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.CourseScheduleService;
import co.fitstart.mobile.service.CoursewareService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.service.WeeklyPlanService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile")
public class CourseController {

//    private static final Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private CourseRoundService courseRoundService;

    @Autowired
    private CourseScheduleService courseScheduleService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    @Autowired
    private WeeklyPlanService weeklyPlanService;

    /**
     * The courseRound list order by course status
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/courses", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView list(@Secure User user) {
        ModelAndView modelAndView = new ModelAndView("course/course-list");

        if(!user.isStudent()) {
            modelAndView.setViewName("course/course-empty");
            return modelAndView;
        }
        
        // 1. load student by userId
        Student student = userService.getStudent(user);
        
        // 2. load all the course round belongs to the student
        List<CourseRound> finishedCourseRounds = courseRoundService.listCourseRound(student, true);
        List<CourseRound> onGoingCourseRounds = courseRoundService.listCourseRound(student, false);
        
        
        modelAndView.addObject("student", student);
        modelAndView.addObject("finishedCourseRounds", finishedCourseRounds);
        modelAndView.addObject("onGoingCourseRounds", onGoingCourseRounds);
        
        return modelAndView;
    }

    /**
     * The courseRound detail to display
     * 
     * @param user
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/courses/{courseId}", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView detail(@Secure User user, 
                               @PathVariable("courseId") Long courseId) {
        
        ModelAndView modelAndView = new ModelAndView();
        
        Student student = userService.getStudent(user);
        CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
        
        if(student == null || !courseRound.matches(student)) {
            modelAndView.setViewName("course/course-error");
            return modelAndView;
        }
        
        if(courseRound.getCourseType().isOnline()) {
            modelAndView.addObject("student", student);
            modelAndView.addObject("courseRound", courseRound);
            
//            boolean needTarget = courseRoundService.needTargetData(student, courseRound);
//            modelAndView.addObject("needTarget", needTarget);
            
            modelAndView.setViewName("course/" + courseRound.getCourseType().getAlias() + "/" + courseRound.getCourseType().getAlias() + "-course-detail");
        } else {
            modelAndView.setViewName("to-weapp");
        }
        
        return modelAndView;
    }
    
    @RequestMapping(value = "/courses/{courseId}/schedules", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView schedules(@Secure User user, 
                               @PathVariable("courseId") Long courseId) {
        
        ModelAndView modelAndView = new ModelAndView();
        
        Student student = userService.getStudent(user);
        CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
        
        if(student == null || !courseRound.matches(student)) {
            modelAndView.setViewName("course/course-error");
            return modelAndView;
        }
        
        modelAndView.addObject("student", student);
        modelAndView.addObject("courseRound", courseRound);
        
        modelAndView.setViewName("course/" + courseRound.getCourseType().getAlias() + "/" + courseRound.getCourseType().getAlias() + "-course-schedule-list");
        
        return modelAndView;
    }
    
    

    /**
     * View courseSchedule detail
     * 
     * @param user 
     * @param scheduleId 
     * @return
     */
    @RequestMapping(value = "/course-schedules/{scheduleId}", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView schedule(@Secure User user,
                                 @PathVariable("scheduleId") Long scheduleId) {
        
        ModelAndView modelAndView = new ModelAndView("course/course-schedule-detail"); 
        
        Student student = userService.getStudent(user);
        
        // 1. load course schedule
        CourseSchedule courseSchedule = courseScheduleService.loadCourseSchedule(scheduleId);
        
        // check student and schedule
        if(student == null || !courseSchedule.matches(student)) {
            modelAndView.setViewName("course/course-error");
            modelAndView.addObject("message", "课程信息与用户信息不匹配");
            return modelAndView;
        }
        
        int session = coursewareService.getRank(courseSchedule);
        List<Courseware> coursewares = coursewareService.listCourseWares(courseSchedule);
        PlannedSchedule plannedSchedule = weeklyPlanService.getPlannedSchedule(courseSchedule, session);
        
        modelAndView.addObject("courseSchedule", courseSchedule);
        modelAndView.addObject("coursewares", coursewares);
        modelAndView.addObject("plannedSchedule", plannedSchedule);
        modelAndView.addObject("session", session);
        
        return modelAndView;
    }
  
}
